import os
import re

import scrapy
import pytesseract
from PIL import Image


class RoomSpider(scrapy.Spider):
    name = 'room'
    allowed_domains = ['ziroom.com']
    start_urls = ['http://cd.ziroom.com/z/']

    # 重写此方法的目的是为了携带请求头headers
    def start_requests(self):
        headers = {
            "Host": "cd.ziroom.com",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36 SLBrowser/6.0.1.9171"
        }

        yield scrapy.Request(url=self.start_urls[0], callback=self.parse, headers=headers)

    def parse(self, response):
        div_list = response.xpath('//div[@class="Z_list-box"]/div')
        img_url = response.xpath('/html/body/section/div[3]/div[2]/div[1]/div[2]/div[2]/span[2]/@style').extract_first()
        new_img_url = re.search(r"(//.*?png)", img_url).group(1)
        new_img_url = "http:" + new_img_url
        yield scrapy.Request(url=new_img_url, callback=self.parse_img)
        for temp in div_list:
            head_title = temp.xpath('.//h5/a/text()').extract_first()
            if head_title:
                title = re.search(r"·(.*?)\d", head_title).group(1)
                type_ = re.search(r"(\d.*?)-", head_title).group(1)
                direction = re.search(r"-(.*)", head_title).group(1)
                desc = temp.xpath('.//div[@class="desc"]/div/text()').extract_first()
                floor = re.search(r" \| (\d*)?/", desc).group(1)
                all_floor = re.search(r" \| \d*?/(\d*)", desc).group(1)
                position = temp.xpath('.//div[@class="location"]/text()').extract_first()
                # 去掉广告
                if position:
                    position = position.strip()
                    # background-image: url(//static8.ziroom.com/phoenix/pc/images/price/new-list/48d4fa3aa7adf78a1feee05d78f33700.png);background-position: -64.2px

                    # if self.real_num:
                    #     price1 = self.real_num[position1]
                    #     price2 = self.real_num[position2]
                    #     price3 = self.real_num[position3]
                    #     price4 = self.real_num[position4]
                    #     price = price1 + price2 + price3 + price4
                # print(title, type_, direction, floor, all_floor, position)
                price_num1 = temp.xpath('.//span[@class="num"][1]/@style').extract_first()
                if price_num1:
                    position1 = re.search(r'position:(.*)', price_num1).group(1).strip()
                    price_num2 = temp.xpath('.//span[@class="num"][2]/@style').extract_first()
                    position2 = re.search(r'position:(.*)', price_num2).group(1).strip()
                    price_num3 = temp.xpath('.//span[@class="num"][3]/@style').extract_first()
                    position3 = re.search(r'position:(.*)', price_num3).group(1).strip()
                    price_num4 = temp.xpath('.//span[@class="num"][4]/@style').extract_first()
                    position4 = re.search(r'position:(.*)', price_num4).group(1).strip()
                    item = {
                        "type": "info",
                        "title": title,
                        "type_": type_,
                        "direction": direction,
                        "floor": floor,
                        "all_floor": all_floor,
                        "position": position,
                        "price": position1 + position2 + position3 + position4
                    }
                    # print(item)
                    yield item

    def parse_img(self, response):
        print("------图片处理的回调函数------")
        # 生成图片信息
        # yield {
        #     "type": "img",
        #     "img_bytes": response.body
        # }
        pytesseract.pytesseract.tesseract_cmd = 'D:\\1234\\tesseract.exe'
        with open("ziru_num.jpg", "wb")as f:
            f.write(response.body)
        img = Image.open('ziru_num.jpg')
        offsets = ['-0px', '-21.4px', '-42.8px', '-64.2px', '-85.6px', '-107px', '-128.4px', '-149.8px', '-171.2px',
                   '-192.6px']
        num = pytesseract.image_to_string(img)

        real_num = {}
        for k, v in zip(offsets, num):
            if v == "\n":
                v = "1"
            real_num[k] = v
        item = {
            "type": "real_num",
            "real_num": real_num
        }
        print(item)
        yield item
